#!/usr/bin/env bash

function runIpython(){

local cmd="${@}"
mkdir -p ${tmpFolder}
settings list &> ${tmpFolder}/settings.set
echo "[_ipy_]" &>> ${tmpFolder}/settings.set
s _ipy_ |sed 's/_ipy_//g' &>> ${tmpFolder}/settings.set

#check ipython version
local ipv parameters
ipv=`ipython -V|cut -d. -f1-2`
if [ "${ipv}" == "0.10" ]; then 
  parameters="-pylab -i -c "
else
  parameters="--pylab -i -c "
 #ipython --pylab -i -c "\"%run /d0/bosmanoglu/projectLocker/adore-doris/trunk/lib/python/ipython.rc '/d1/batu/playGround/livTest/tmp/settings.set' \""
fi

if [ -z "${cmd}" ]; then 
  if [ "${g-notGui}" != "notGui" ]; then
    [ "${ipv}" == "0.10" ] && ipython -pylab -i -c "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'"
    [ "${ipv}" != "0.10" ] && ipython qtconsole --pylab -c "\"%run $ADOREFOLDER/lib/python/ipython.rc "${tmpFolder}/settings.set" \""  &
  else
    [ "${ipv}" == "0.10" ] && ipython -pylab -i -c "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'"
    [ "${ipv}" != "0.10" ] && ipython --pylab -i -c "%run $ADOREFOLDER/lib/python/ipython.rc ${tmpFolder}/settings.set"   
  fi
elif [ -f "${cmd}" ]; then
  #echo "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'" > tmpfile.tmp
  #cat "${cmd}" >> tmpfile.tmp
  #ipython -pylab tmpfile.tmp
  [ "${ipv}" == "0.10" ] && cat "${cmd}" | ipython -pylab -pi1 " " -i -c "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'" 
  [ "${ipv}" != "0.10" ] && cat "${cmd}" | ipython --pylab -i -c "\"%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set' \""   
  #ipython -pylab -i -c "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'" 
else
  [ "${ipv}" == "0.10" ] && (echo "${cmd}" ) | ipython -pylab -i -c "%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set'"  
  [ "${ipv}" != "0.10" ] && (echo "${cmd}" ) | ipython --pylab -i -c "\"%run $ADOREFOLDER/lib/python/ipython.rc '${tmpFolder}/settings.set' \""
fi
#if [ -e "${i_resfile}" ]; then
#  ipython -pylab -i -c "import os; os.sys.path.append(os.path.join(os.path.dirname(os.environ['ADORESCR']), 'lib/python')); import adore; ires=adore.res2dict('${i_resfile}');mres=adore.res2dict('${m_resfile}');sres=adore.res2dict('${s_resfile}');settings=adore.parseSettings('${tmpFolder}/settings.set')"
#else
#  ipython -pylab -i -c "import os; os.sys.path.append(os.path.join(os.path.dirname(os.environ['ADORESCR']), 'lib/python')); import adore; mres=adore.res2dict('${m_resfile}');sres=adore.res2dict('${s_resfile}');"
#fi
}

####### MAIN
runIpython ${@}